<template>
  <div class="chart-right">
    <div class="item">
      <div class="header">
        <div class="left">
          <i><i class="blue"></i><i class="blue-transparent"></i></i>
          <span>客户交易频次Top20</span>
        </div>
        <div class="right"></div>
      </div>
      <div class="chart" style="height: 90%">
        <div
          id="echartsHorizontalBar"
          ref="echartsHorizontalBar"
          class="box"
          style="height: 90%"
        ></div>
      </div>
    </div>
    <div class="item">
      <div class="header">
        <div class="left">
          <i><i class="blue"></i><i class="blue-transparent"></i></i>
          <span>客户交易量TOP10</span>
        </div>
        <div class="right"></div>
      </div>
      <div class="chart" style="height: 90%">
        <div
          id="echartsHorizontalBarkhjyl"
          ref="echartsHorizontalBarkhjyl"
          class="box"
          style="height: 90%"
        ></div>
      </div>
    </div>
    <div class="item">
      <div class="header">
        <div class="left">
          <i><i class="blue"></i><i class="blue-transparent"></i></i>
          <span>核心客户交易量品种分布</span>
        </div>
        <div class="right"></div>
      </div>
      <div class="chart" style="height: 90%">
        <CoreCustomersPie
          :id="'CoreCustomersPie'"
          :width="'100%'"
          :height="'90%'"
          style="height: 90%"
        />
      </div>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts';
import CoreCustomersPie from './CoreCustomersPie.vue';
import { GetFrequencyTopApi } from '../../../api/customerManagement/informationOverview.js';
import { getQuantityTop } from '@/api/customerList/index.js';
export default {
  components: {
    CoreCustomersPie,
  },
  props: {},
  data() {
    return {
      quantityTopList: [],
    };
  },
  async created() {},
  mounted() {
    this.loadKHJYPCDData();
    // window.onresize = () => {
    //   this.getKHJYPCData();
    //   this.getKHJYLData();
    // };
    // window.addEventListener("resize", () => {
    //   this.getKHJYPCData();
    //   this.getKHJYLData();
    // });
    this.getKHJYPCData();
    this.getKHJYLData();
  },
  methods: {
    async loadKHJYPCDData() {
      const { data } = await GetFrequencyTopApi();
      this.getKHJYPCData(data);
      console.log(data);
    },
    //客户交易频次
    getKHJYPCData(data) {
      let xAxisData = [];
      let yAxisData = [];
      Array.from(data || []).forEach((item) => {
        xAxisData.push(item.custName);
        yAxisData.push(item.tradeFrequency);
      });
      const options = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
          width: '700px',
        },
        xAxis: {
          type: 'value',
          name: '（次）',
          nameTextStyle: {
            color: '#C1C9D8', // 设置名称颜色为白色
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
              color: '#AFB0B3',
            },
          },
          axisLine: {
            lineStyle: {
              color: '#60686F', // Y 轴轴线颜色
            },
          },
          axisLabel: {
            textStyle: {
              color: '#C1C9D8', // Y 轴文字颜色
            },
          },
        },
        yAxis: {
          type: 'category',

          axisLine: {
            lineStyle: {
              color: '#60686F', // Y 轴轴线颜色
            },
          },
          axisLabel: {
            textStyle: {
              color: '#C1C9D8', // Y 轴文字颜色
            },
            formatter: (val) => {
              // console.log('formatter',val);
              // let c = document.createElement('canvas');
              // const ctx = c.getContext('2d');
              // ctx.font = '16px normal'; // 设置画布内的字体，与设置的textStyle一致
              // const arr = val.split('');
              // arr
              //   .map((item) => ctx.measureText(item).width)
              //   .reduce((pre, next, index) => {
              //     const nLen = pre + next;
              //     if (nLen > 100) {
              //       arr[index - 1] += '\n';
              //       return next;
              //     } else {
              //       return nLen;
              //     }
              //   });
              // c = null;
              // console.log(arr);
              // return arr.join('');
              if (val.length > 6) {
                return val.substring(0, 6) + '...';
              } else {
                return val;
              }
            },
          },

          data: xAxisData,
        },
        series: [
          {
            name: '2011',
            type: 'bar',
            barWidth: 8,
            itemStyle: {
              emphasis: {
                color: [
                  { offset: 0, color: '#8B99B2' }, // 渐变起始颜色
                  { offset: 0.6, color: 'rgba(139,153,178,0.4)' }, // 渐变起始颜色
                  { offset: 1, color: '#ffffff' }, // 渐变结束颜色
                ], // 鼠标经过状态下的背景色
              },
              color: {
                type: 'linear',
                colorStops: [
                  { offset: 0, color: '#8B99B2' }, // 渐变起始颜色
                  { offset: 0.6, color: 'rgba(139,153,178,0.4)' }, // 渐变起始颜色
                  { offset: 1, color: '#ffffff' }, // 渐变结束颜色
                ],
              },
            },
            data: yAxisData,
          },
        ],
      };

      this.echarts1 = echarts.init(
        document.getElementById('echartsHorizontalBar')
      );
      // console.log("echarts options", this.echartsData, options);
      this.echarts1.setOption(options, true);
      setTimeout(() => {
        this.echarts1.resize();
      }, 500);
    },
    //客户交易量
    async getKHJYLData() {
      let xAxisData = [];
      let yAxisData = [];
      const { data } = await getQuantityTop();
      Array.from(data || []).forEach((item) => {
        xAxisData.push(item.company);
        yAxisData.push(item.notional);
      });
      const options = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: {
          type: 'value',
          name: '（次）',
          nameTextStyle: {
            color: '#C1C9D8', // 设置名称颜色为白色
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
            },
          },
          axisLine: {
            lineStyle: {
              color: '#60686F', // Y 轴轴线颜色
            },
          },
          axisLabel: {
            textStyle: {
              color: '#C1C9D8', // Y 轴文字颜色
            },
          },
        },
        yAxis: {
          type: 'category',

          axisLine: {
            lineStyle: {
              color: '#60686F', // Y 轴轴线颜色
            },
          },
          axisLabel: {
            textStyle: {
              color: '#fff', // Y 轴文字颜色
            },
          },
          data: xAxisData,
        },
        series: [
          {
            name: '2011',
            type: 'bar',
            barWidth: 8,
            itemStyle: {
              emphasis: {
                color: [
                  { offset: 0, color: 'rgba(47,163,255,0.31)' }, // 渐变起始颜色
                  { offset: 0.6, color: '#2FA3FF' }, // 渐变起始颜色
                  { offset: 1, color: '#E9F0F5' }, // 渐变结束颜色
                ], // 鼠标经过状态下的背景色
              },
              color: {
                type: 'linear',
                colorStops: [
                  { offset: 0, color: 'rgba(47,163,255,0.31)' }, // 渐变起始颜色
                  { offset: 0.6, color: '#2FA3FF' }, // 渐变起始颜色
                  { offset: 1, color: '#E9F0F5' }, // 渐变结束颜色
                ],
              },
            },
            data: yAxisData,
          },
        ],
      };

      this.echarts1 = echarts.init(
        document.getElementById('echartsHorizontalBarkhjyl')
      );
      // console.log("echarts options", this.echartsData, options);
      this.echarts1.setOption(options, true);
      setTimeout(() => {
        this.echarts1.resize();
      }, 500);
    },
    toCustomerView() {
      this.$router.replace('/customerManagement/customerView');
    },
    pageFn() {},
  },
};
</script>
<style lang="scss" scoped>
.chart-right {
  width: 100%;
  height: 100%;
  padding: 10px;
  .item {
    width: 100%;
    height: 33.3%;
    > .header {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #4a4f60;
      > .left {
        display: flex;
        align-items: center;
        i {
          height: 32px;
          width: 15px;
          display: flex;
          align-items: center;
          .blue {
            width: 3px;
            height: 32px;
            background-color: #2fa3ff;
          }
          .blue-transparent {
            width: 12px;
            height: 32px;
            background: rgba(47, 163, 255, 0.16);
          }
        }
        span {
          font-family: PingFang-SC, PingFang-SC;
          font-weight: bold;
          font-size: 16px;
          color: #ffffff;
          margin-left: 10px;
        }
      }
      .right {
        position: relative;
        width: 6px;
        height: 6px;
        background-color: #2fa3ff;
        border-radius: 50%;
      }

      .right::before {
        content: '';
        position: absolute;
        top: -3px;
        left: -3px;
        width: 12px;
        height: 12px;
        background-color: #2fa3ff;
        border-radius: 50%;
        opacity: 0;
        animation: expand 1s linear infinite;
      }

      @keyframes expand {
        0% {
          opacity: 0;
          transform: scale(0);
        }
        50% {
          opacity: 0.5;
          transform: scale(1);
        }
        100% {
          opacity: 0;
          transform: scale(2);
        }
      }
    }
    // 解决echarts resize不生效问题
    ::v-deep.box {
      width: 100%;
      height: 100%;
      min-height: 240px;
      // 子选择器(>) :只能选择作为某元素儿子元素的元素（直接子元素），不包括孙元素、曾孙元素等等等
      & > div:first-child {
        width: 100% !important;
        height: 100% !important;
        min-height: 240px;
        canvas {
          width: 100% !important;
          height: 100% !important;
          min-height: 240px;
        }
      }
    }
  }
}
</style>
